<template>
  <div class="app-container">
    <!-- Search Form -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="装备品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入装备品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="装备型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入装备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存数量" prop="stock">
        <el-input
          v-model="queryParams.stock"
          placeholder="请输入库存数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <!-- Action Buttons -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="3">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['HubSys:equipment:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['HubSys:equipment:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['HubSys:equipment:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['HubSys:equipment:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- Data Table -->
    <el-table
      v-loading="loading"
      :data="equipmentList"
      @selection-change="handleSelectionChange"
      border
      stripe
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="装备品牌" align="center" prop="brand" />
      <el-table-column label="装备型号" align="center" prop="model" />
      <el-table-column label="装备规格" align="center" prop="specifications" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="库存数量" align="center" prop="stock" />
      <el-table-column label="" align="center" prop="imageurl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageurl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="装备描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['HubSys:equipment:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['HubSys:equipment:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- Pagination -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- Add or Edit Dialog -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="装备品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入装备品牌" />
        </el-form-item>
        <el-form-item label="装备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入装备型号" />
        </el-form-item>
        <el-form-item label="装备规格" prop="specifications">
          <el-input v-model="form.specifications" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="库存数量" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="商品图片" prop="imageurl">
          <image-upload v-model="form.imageurl" action="/dev-api/HubSys/equipment/upload" />
        </el-form-item>
        <el-form-item label="装备描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEquipment,
  getEquipment,
  delEquipment,
  addEquipment,
  updateEquipment,
} from '@/api/HubSys/equipment';

export default {
  name: 'Equipment',
  data() {
    return {
      loading: true,
      equipmentList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brand: null,
        model: null,
        price: null,
        stock: null,
        imageurl: '',
        description: null,
      },
      form: {
        equipmentid: null,
        brand: null,
        model: null,
        specifications: null,
        price: null,
        stock: null,
        imageurl: null,
        description: null,
      },
      rules: {
        brand: [{ required: true, message: '装备品牌不能为空', trigger: 'blur' }],
        model: [{ required: true, message: '装备型号不能为空', trigger: 'blur' }],
        specifications: [{ required: true, message: '装备规格不能为空', trigger: 'blur' }],
        price: [{ required: true, message: '价格不能为空', trigger: 'blur' }],
        stock: [{ required: true, message: '库存数量不能为空', trigger: 'blur' }],
      },
      title: '',
      open: false,
      ids: [],
      single: true,
      multiple: true,
      total: 0,
      showSearch: true,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then((response) => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        equipmentid: null,
        brand: null,
        model: null,
        specifications: null,
        price: null,
        stock: null,
        imageurl: null,
        description: null,
      };
      this.resetForm('form');
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.equipmentid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加装备列表';
    },
    handleUpdate(row) {
      this.reset();
      const equipmentid = row.equipmentid || this.ids;
      getEquipment(equipmentid).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = '修改装备列表';
      });
    },
    handleDelete(row) {
      const equipmentids = row.equipmentid || this.ids;
      this.$modal.confirm(`是否确认删除装备列表编号为"${equipmentids}"的数据项？`)
        .then(() => delEquipment(equipmentids))
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.equipmentid != null) {
            updateEquipment(this.form).then(() => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(() => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleExport() {
      this.download('HubSys/equipment/export', {
        ...this.queryParams,
      }, `equipment_${new Date().getTime()}.xlsx`);
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
.small-padding {
  padding: 0 5px;
}
.fixed-width {
  width: 120px;
}
</style>
